package com.example.task1.ui.AllService.library;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.task1.R;
import com.example.task1.application.MyApplication;
import com.example.task1.bean.LibraryCommentBean;
import com.example.task1.http.Api;
import com.example.task1.ui.BaseAdapter;
import com.example.task1.util.ApiResp;
import com.example.task1.util.CallBack;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import retrofit2.Call;

public class LibraryCommentAdapter extends BaseAdapter<LibraryCommentBean.DataDTO> {

    private Context context;
    private List<LibraryCommentBean.DataDTO> mData = new ArrayList<>();
    private TextView tvName;
    private ImageView image;
    private TextView content;
    private TextView tvTime;

    public LibraryCommentAdapter(Context context) {
        this.context = context;
    }

    @Override
    protected void setData(List<LibraryCommentBean.DataDTO> rows) {
        int size = mData.size();
        mData.addAll(rows);
        notifyItemInserted(size);
    }

    @Override
    protected int getLayout() {
        return R.layout.librarycommentlist;
    }

    @Override
    protected Context getContext() {
        return null;
    }

    @Override
    protected List<LibraryCommentBean.DataDTO> getData() {
        return mData;
    }

    @Override
    protected void onBind(myHolder holder, LibraryCommentBean.DataDTO date) {
        tvName.setText(date.getUserName());
        tvTime.setText(date.getCreateTime());
        content.setText(date.getContent());
        if (date.isMyLikeState()) {
            image.setImageResource(R.drawable.ic_baseline_favorite_red_24);
        }
        image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HashMap<String, Object> map = new HashMap<>();
                map.put("commentId", date.getId());
                map.put("like", true);
                Call<ApiResp> post = Api.api().post(MyApplication.TOKEN, "", Api.getBody(map));
                Api.send(post, ApiResp.class, new CallBack() {
                    @Override
                    public void success(Object data) {
                        image.setImageResource(R.drawable.ic_baseline_favorite_red_24);
                        MyApplication.show("点赞成功");
                    }

                    @Override
                    public void error(String msg) {

                    }
                });
            }
        });
    }

    @Override
    protected void holder(View view) {
        initView(view);
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    private void initView(View view) {
        tvName = view.findViewById(R.id.tv_name);
        image = view.findViewById(R.id.image);
        content = view.findViewById(R.id.content);
        tvTime = view.findViewById(R.id.tv_time);
    }
}
